package controllers.bbs;

import java.util.List;

import models.bbs.Article;
import models.bbs.Pager;

import org.bson.types.ObjectId;

import controllers.Secure;

import play.data.validation.Required;
import play.mvc.Controller;
import play.mvc.With;

public class Press extends Controller {
	
	public static void list(int page){
		if(page == 0) page = 1;
		
		System.out.println("page : " + page);
		int offset = (page - 1) * Pager.rowsPerPages;
		List<Article> articles = Article.find().order("-createdDateTime").offset(offset).limit(Pager.rowsPerPages).asList();
		Pager pager = new Pager("/board/press/list.html", Article.find().count(), page);
		render(articles, pager);
	}
	
	public static void view(ObjectId id){
		Article article = Article.findById(id);
		System.out.println(session.contains("username"));
		if(!session.contains("username") || !article.getAuthor().equals(session.get("username"))){
			System.out.println("hit");
			article.hit();
		}
		article.save();
		render(article);
	}
	
	public static void delete(ObjectId id){
		Article article = Article.findById(id);
		article.delete();
		list(0);
	}
	
	public static void write(){
		render();
	}
	
	public static void writeb(@Required String title, @Required String content, @Required String author){
		if(validation.hasErrors()) {
		       params.flash(); 
		       validation.keep(); 
		       write();
		 }
		Article article = new Article(title, content, author);
		article.save();
//		
		list(0);
	}
}
